﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class YARNREQUESTReport : BaseReport
    {
        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
        string Standard = "YARNREQUEST";
        string filePath = @"C:\intex.w\Report\HLT\HLTPlanningReport.xls";
        public YARNREQUESTReport()
            : base()
        {
        }
        public YARNREQUESTReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            strExcelFile = @"HLT\HLTInventory.xls";
            #region Excel  columns
            columns.Add("GAAUFKRS");
            columns.Add("GAAUFLFD");
            columns.Add("GAAUFPOS");
            columns.Add("ABPNEUDAT");
            columns.Add("FACTORY");
            columns.Add("TYPE");
            columns.Add("FABRIC");
            columns.Add("GAART");
            columns.Add("GSGARNBEZ");
            columns.Add("GAFARBE");
            columns.Add("GSCBEZ");
            columns.Add("REQWEI");
            #endregion
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["GAAUFYARN"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (DataColumn dcTemp in dtSoruce.Columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[dcTemp.ColumnName].ToString());
                        y++;
                    }
                    x++;
                }
               /* x = 505;
                yStart = 1;
                dtSoruce = setSoruce.Tables["WBBEW"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                }
                HideNullRows();
                 */
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void FillInExcelData1(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["YARNREQSUM"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (DataColumn dcTemp in dtSoruce.Columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[dcTemp.ColumnName].ToString());
                        y++;
                    }
                    x++;
                }
                /* x = 505;
                 yStart = 1;
                 dtSoruce = setSoruce.Tables["WBBEW"];
                 foreach (DataRow dataRow in dtSoruce.Rows)
                 {
                     y = yStart;
                     foreach (string column in columns)
                     {
                         exAgent.SetCellValue(x, y, dataRow[column].ToString());
                         y++;
                     }
                     x++;
                 }
                 HideNullRows();
                  */
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);

                rowstart = 5; rowend = 500; col1 = 9; col2 = 10;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, col1, col2).Trim();
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "YARNREQUEST";
            strExcelFile = @"HLT\HLTPlanningReport.xls";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Planning\原料申购分析" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(@":", ""); 
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(columns, dsSource, 2, 1);
            base.WriteExcelReport(fileType);
          /* export to requisition form
            Standard = "REQFORM";
            strExcelFile = @"HLT\HLTPlanningReport.xls";
            filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Planning\原料申购单" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData1(columns, dsSource, 2, 2);
            base.WriteExcelReport(fileType);*/

        }
        public  void WriteExcelReport1(string fileType)
        {
            string filename = "";
            string sheetName = "";
            
            /* export to requisition form*/
            string Standard = "REQFORM";
            strExcelFile = @"HLT\HLTPlanningReport.xls";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Planning\原料申购单" + filedate + ".xls";
            sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData1(columns, dsSource, 2, 2);
            base.WriteExcelReport(fileType);

        }
    }
}
